package com.gaohe.web.controller.scp;

import java.util.List;
import java.util.Objects;

import com.gaohe.common.exception.ServiceException;
import com.gaohe.common.utils.SecurityUtils;
import com.gaohe.scp.domain.vo.CarAllInfoDTO;
import com.gaohe.scp.domain.vo.CsCarDto;
import com.gaohe.scp.domain.vo.CsCarVo;
import com.gaohe.scp.domain.vo.CsNoticeVo;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gaohe.common.annotation.Log;
import com.gaohe.common.core.controller.BaseController;
import com.gaohe.common.core.domain.AjaxResult;
import com.gaohe.common.enums.BusinessType;
import com.gaohe.scp.domain.CsCar;
import com.gaohe.scp.service.ICsCarService;
import com.gaohe.common.utils.poi.ExcelUtil;
import com.gaohe.common.core.page.TableDataInfo;

/**
 * 车辆管理Controller
 *
 * @author liuxiangyu
 * @date 2025-03-28
 */
@RestController
@RequestMapping("/scp/car")
public class CsCarController extends BaseController
{
    @Autowired
    private ICsCarService csCarService;

    /**
     * 查询车辆管理列表
     */
//    @PreAuthorize("@ss.hasPermi('scp:car:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsCar csCar)
    {
        startPage();
        List<CsCar> list = csCarService.selectCsCarList(csCar);
        return getDataTable(list);
    }

    /**
     * 作者:张文豪
     * 管理端
     * 查询车辆管理列表
     */
//    @PreAuthorize("@ss.hasPermi('scp:car:list')")
    @GetMapping("/list/gld")
    public TableDataInfo GldSelectCsCarList(CsCar csCar)
    {
        startPage();
        List<CsCar> list = csCarService.GldSelectCsCarList(csCar);
        return getDataTable(list);
    }

    /**
     * 导出车辆管理列表
     */
//    @PreAuthorize("@ss.hasPermi('scp:car:export')")
    @Log(title = "车辆管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsCar csCar)
    {
        List<CsCar> list = csCarService.selectCsCarList(csCar);
        ExcelUtil<CsCar> util = new ExcelUtil<CsCar>(CsCar.class);
        util.exportExcel(response, list, "车辆管理数据");
    }

    /**
     * 获取车辆管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('scp:car:query')")
    @GetMapping(value = "/{carId}")
    public AjaxResult getInfo(@PathVariable("carId") Long carId)
    {
        return success(csCarService.selectCsCarByCarId(carId));
    }

    /**
     * 管理端
     * 作者:张文豪
     * 获取车辆管理详细信息
     * return 包含carAllInfoDto的封装结果集
     */
//    @PreAuthorize("@ss.hasPermi('scp:car:query')")
    @GetMapping(value = "/gld/{carId}")
    public AjaxResult GldGetInfo(@PathVariable("carId") Long carId)
    {
        return success(csCarService.selectCarAllInfoDto(carId));
    }

    /* * 新增车辆
     * @param csCar
     * @return
     * 新增车辆管理
    */
    @PostMapping
    public AjaxResult add(@RequestBody CsCar csCar)
    {
        return toAjax(csCarService.insertCsCar(csCar));
    }

    /**
     * 管理端
     * 作者:张文豪
     * 新增车辆管理
     * @param dto
     * @return 1
     */
    @PostMapping("/gld")
    public AjaxResult addAllCarInfo(@RequestBody CarAllInfoDTO dto) {

        try {
            return toAjax(csCarService.insertAllCarInfo(dto));
        } catch (ServiceException e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
     * 修改车辆管理
     */
//    @PreAuthorize("@ss.hasPermi('scp:car:edit')")
    @Log(title = "车辆管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsCar csCar)
    {
        return toAjax(csCarService.updateCsCar(csCar));
    }

    /**
     * 管理端
     * 作者:张文豪
     * @param dto
     * @return 1
     */
//    @PreAuthorize("@ss.hasPermi('scp:car:edit')")
    @Log(title = "车辆管理", businessType = BusinessType.UPDATE)
    @PutMapping("/gld")
    public AjaxResult GldEdit(@RequestBody CarAllInfoDTO dto)
    {
        System.err.println(dto.getStartTime());
        return toAjax(csCarService.updateAllCarInfo(dto));
    }

    /**
     * 作者:张文豪
     * 车辆逻辑删除
     * @param carId
     * @return 1
     */
    @DeleteMapping("/gld/{carId}")
    public AjaxResult deleteAllCarInfo(@PathVariable Long carId){
        return toAjax(csCarService.delAllCarInfo(carId));
    }

    /**
     * 删除车辆管理
     */
//    @PreAuthorize("@ss.hasPermi('scp:car:remove')")
    @Log(title = "车辆管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{carIds}")
    public AjaxResult remove(@PathVariable Long[] carIds)
    {
        return toAjax(csCarService.deleteCsCarByCarIds(carIds));
    }
    //往期竞拍、在售车辆
    @GetMapping("/wangqiAndMaiIng")
    public TableDataInfo selectWangqiAndMaiIng(CsCar csCar)
    {
        startPage();
        List<CsCarVo> csCarVos = csCarService.selectWangqiAndMaiIng(csCar);
        return getDataTable(csCarVos);
    }
    //我的关注
    @GetMapping("/myGuanZhu")
    public TableDataInfo selectMyGuanZhu()
    {
        Long userId = SecurityUtils.getUserId();
        System.err.println(userId);
        startPage();
        List<CsCarVo> csCarVos = csCarService.selectMyGuanZhu(userId);
        return getDataTable(csCarVos);
    }
    //今日竞拍
    @GetMapping("/todayJingPai")
    public TableDataInfo todayJingPai(CsCar csCar)
    {
        startPage();
        List<CsCarVo> csCarVos = csCarService.todayJingPai(csCar);
        return getDataTable(csCarVos);

    }
    //通过搜索条件查询车辆
    @GetMapping("/searchCar")
    public TableDataInfo searchCar(CsCarVo csCarVo)
    {
        startPage();
        List<CsCarVo> csCarVos = csCarService.searchCar(csCarVo);
        return getDataTable(csCarVos);
    }

    //全部车源
    @GetMapping("/allCar")
    public TableDataInfo allCar(CsCarDto csCarDto)
    {
        String status = csCarDto.getStatus();
        String order = csCarDto.getOrder();
        if (Objects.equals(status, "全部")) {
            csCarDto.setStatus(null);
        }
        if (Objects.equals(order, "价格")) {
            csCarDto.setOrder(null);
        }
        startPage();
        List<CsCarVo> csCarVos = csCarService.allCar(csCarDto);
        return getDataTable(csCarVos);
    }

    //预约列表
    @GetMapping("/myReserve")
    public TableDataInfo myReserve(CsCar csCar)
    {
        //获取当前登录用户
        Long userId = SecurityUtils.getUserId();
        System.err.println(userId);
        System.err.println(csCar.getCarName());
        csCar.setUserId(userId);
        startPage();
        List<CsCarVo> csCarVos = csCarService.myReserve(csCar);

        return getDataTable(csCarVos);
    }
    //修改预约状态
    @PutMapping("/reserverDetail/{id}")
    public AjaxResult updateReserverDetail(@PathVariable Long id){

        return toAjax(csCarService.updateReserverDetail(id));
    }
    //查询预约状态
    @GetMapping("/selectStatus/{id}")
    public CsCar selectReserverType(@PathVariable Long id){
        return csCarService.selectReserverStatus(id);
    }





}
